<template>
  <div class="app-container">
    <div class="search-bar">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item prop="logType" label="日志类型">
          <el-input
            v-model="queryParams.logType"
            placeholder="日志类型"
            clearable
            style="width: 190px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item prop="title" label="日志标题">
          <el-input
            v-model="queryParams.title"
            placeholder="日志标题"
            clearable
            style="width: 190px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item prop="serviceId" label="服务ID">
          <el-input
            v-model="queryParams.serviceId"
            placeholder="服务ID"
            clearable
            style="width: 190px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item prop="remoteAddr" label="远程地址">
          <el-input
            v-model="queryParams.remoteAddr"
            placeholder="远程地址"
            clearable
            style="width: 190px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item prop="userAgent" label="用户代理">
          <el-input
            v-model="queryParams.userAgent"
            placeholder="用户代理"
            clearable
            style="width: 190px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item prop="requestUri" label="请求URL">
          <el-input
            v-model="queryParams.requestUri"
            placeholder="请求URL"
            clearable
            style="width: 190px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item prop="method" label="请求方法">
          <el-input
            v-model="queryParams.method"
            placeholder="请求方法"
            clearable
            style="width: 190px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item prop="params" label="请求参数">
          <el-input
            v-model="queryParams.params"
            placeholder="请求参数"
            clearable
            style="width: 190px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item prop="exception" label="异常信息">
          <el-input
            v-model="queryParams.exception"
            placeholder="异常信息"
            clearable
            style="width: 190px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="search" @click="handleQuery">搜索</el-button>
          <el-button icon="refresh" @click="handleResetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never">
      <div class="mb-10px">
        <!-- <el-button type="success" icon="plus" @click="dialog.visible = true">插入日志</el-button> -->
        <el-button type="danger" icon="delete" @click="deleteLogs">删除</el-button>
        <el-button type="primary" @click="exportExcel">导出日志</el-button>
      </div>
      <el-table
        v-loading="loading"
        :data="pageData"
        highlight-current-row
        border
        @selection-change="selectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column label="日志类型" prop="logType" min-width="100" />
        <el-table-column label="日志标题" prop="title" min-width="100" />
        <el-table-column label="服务ID" prop="serviceId" min-width="100" />
        <el-table-column label="远程地址" prop="remoteAddr" min-width="100" />
        <el-table-column label="用户代理" prop="userAgent" min-width="100" />
        <el-table-column label="请求URL" prop="requestUri" min-width="100" />
        <el-table-column label="请求方法" prop="method" min-width="100" />
        <el-table-column label="请求参数" prop="params" min-width="100" />
        <el-table-column label="执行时间" prop="time" min-width="100" />
        <el-table-column label="异常信息" prop="exception" min-width="100" />
        <el-table-column label="创建人" prop="createBy" min-width="100" />
        <el-table-column label="创建时间" prop="createTime" min-width="100" />
        <el-table-column label="修改人" prop="updateBy" min-width="100" />
        <el-table-column label="修改时间" prop="updateTime" min-width="100" />
      </el-table>
      <pagination
        v-if="queryParams.total > 0"
        v-model:total="queryParams.total"
        v-model:page="queryParams.current"
        v-model:limit="queryParams.size"
        @pagination="handleQuery"
      />
    </el-card>
    <el-dialog
      v-model="dialog.visible"
      :title="dialog.title"
      width="400px"
      @closed="handleCloseDialog"
    >
      <el-form ref="deptFormRef" :model="formData" :rules="rules" label-width="80px">
        <el-form-item prop="logType" label="日志类型">
          <el-input v-model="formData.logType" placeholder="日志类型" />
        </el-form-item>
        <el-form-item prop="title" label="日志标题">
          <el-input v-model="formData.title" placeholder="日志标题" />
        </el-form-item>
        <el-form-item prop="serviceId" label="服务ID">
          <el-input v-model="formData.serviceId" placeholder="服务ID" />
        </el-form-item>
        <el-form-item prop="remoteAddr" label="远程地址">
          <el-input v-model="formData.remoteAddr" placeholder="远程地址" />
        </el-form-item>
        <el-form-item prop="userAgent" label="用户代理">
          <el-input v-model="formData.userAgent" placeholder="用户代理" />
        </el-form-item>
        <el-form-item prop="requestUri" label="请求URL">
          <el-input v-model="formData.requestUri" placeholder="请求URL" />
        </el-form-item>
        <el-form-item prop="method" label="请求方法">
          <el-input v-model="formData.method" placeholder="请求方法" />
        </el-form-item>
        <el-form-item prop="params" label="请求参数">
          <el-input v-model="formData.params" placeholder="请求参数" />
        </el-form-item>
        <el-form-item prop="time" label="执行时间">
          <el-input v-model="formData.time" placeholder="执行时间" />
        </el-form-item>
        <el-form-item prop="exception" label="异常信息">
          <el-input v-model="formData.exception" placeholder="异常信息" />
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
          <el-button @click="handleCloseDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
defineOptions({
  name: 'Log',
  inheritAttrs: false,
});

import LogAPI, { LogPageVO, LogPageQuery } from '@/api/system/log.api';
const queryFormRef = ref();

const loading = ref(false);

const dialog = reactive({
  title: '新增',
  visible: false,
});

const rules = reactive<any>({
  logType: [{ required: true, message: '日志类型不能为空', trigger: 'blur' }],
  title: [{ required: true, message: '日志标题不能为空', trigger: 'blur' }],
  serviceId: [{ required: true, message: '服务ID不能为空', trigger: 'blur' }],
  remoteAddr: [{ required: true, message: '远程地址不能为空', trigger: 'blur' }],
  userAgent: [{ required: true, message: '用户代理不能为空', trigger: 'blur' }],
  requestUri: [{ required: true, message: '请求URL不能为空', trigger: 'blur' }],
  method: [{ required: true, message: '请求方法不能为空', trigger: 'blur' }],
});
const formData = ref<LogPageVO>({});
const queryParams = reactive<LogPageQuery>({
  logType: undefined,
  title: undefined,
  serviceId: undefined,
  remoteAddr: undefined,
  userAgent: undefined,
  requestUri: undefined,
  method: undefined,
  params: undefined,
  exception: undefined,
  current: 1,
  size: 10,
  total: 0,
});

function handleSubmit() {
  queryFormRef.value.validate((valid: any) => {
    if (!valid) {
      return;
    }
    LogAPI.createLog(formData.value).then(() => {
      ElMessage.success('新增成功');
      handleCloseDialog();
      handleQuery();
    });
  });
}

function handleCloseDialog() {
  dialog.visible = false;
  formData.value = {};
}
// 批量删除
const selectionIds = ref<string[]>([]);
function deleteLogs() {
  if (selectionIds.value.length === 0) {
    ElMessage.warning('请选择要删除的日志');
    return;
  }
  ElMessageBox.confirm('确认删除选中的日志吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    LogAPI.deleteLogs(selectionIds.value).then(() => {
      ElMessage.success('删除成功');
      handleQuery();
    });
  });
}
function selectionChange(val: any) {
  selectionIds.value = val.map((item: any) => item.id);
}

// 导出日志
function exportExcel() {
  const params: any = toRaw(queryParams);
  for (const key in params) {
    if (params[key] === undefined || params[key] === '') {
      delete params[key];
    }
  }
  LogAPI.exportLog(params).then((res: any) => {
    const url = window.URL.createObjectURL(res.data);
    window.open(url, '_blank');
  });
}
// 日志表格数据
const pageData = ref<LogPageVO[]>();

/** 查询 */
function handleQuery() {
  loading.value = true;
  const params: any = toRaw(queryParams);
  for (const key in params) {
    if (params[key] === undefined || params[key] === '') {
      delete params[key];
    }
  }
  LogAPI.getPage(queryParams)
    .then((res: any) => {
      pageData.value = res.records;
      queryParams.total = res.total;
    })
    .finally(() => {
      loading.value = false;
    });
}
/** 重置查询 */
function handleResetQuery() {
  queryFormRef.value.resetFields();
  queryParams.current = 1;
  handleQuery();
}

onMounted(() => {
  handleQuery();
});
</script>
